#include "UsingBuilder.h"

#include "ScriptCompiler/ScriptFileContext.h"


CUsingBuilder::CUsingBuilder(std::shared_ptr<CScriptFileContext> InContext)
{
    Context = InContext;
    Result = std::make_shared<CUsingNode>(InContext);

}

bool CUsingBuilder::ReadCurentToken()
{
    //check and skip 'using'
    if (Context->GetCurrentToken()->Type != EScriptTokenType::Using)
    {
        Context->RaiseError(10132,Context->GetCurrentToken(),U"Expected 'using' keyword");
        return false;
    }
    Result->Using = Context->GetCurrentToken();
    Context->CurrentTokenIndex++;

    //build name chain
    {
        auto Token = Context->GetCurrentToken();
        if (Token->Type == EScriptTokenType::Identifier)
        {
            Result->NameChain.Add(Token);
            Context->CurrentTokenIndex++;
        }
        else
        {
            Context->RaiseError(10133,Token,U"Expected identifier after 'using' keyword");
            return false;
        }

        while (true)
        {
            Token = Context->GetCurrentToken();
            if (Token->Type == EScriptTokenType::Dot)
            {
                Context->CurrentTokenIndex++; //skip '.'
            }
            else
            {
                break;
            }

            Token = Context->GetCurrentToken();
            if (Token->Type == EScriptTokenType::Identifier)
            {
                Result->NameChain.Add(Token);
                Context->CurrentTokenIndex++;
            }
            else
            {
                Context->RaiseError(10134,Token,U"Expected identifier after '.'");
                return false;
            }
        }
    }

    //check and skip ';'
    {
        auto Token = Context->GetCurrentToken();
        if (Token->Type == EScriptTokenType::Semicolon)
        {
            Context->CurrentTokenIndex++; //skip ';'
        }
        else
        {
            Context->RaiseError(10135,Token,U"Expected ';' after 'using' statement");
            return false;
        }
    }

    return true;

}
